<template>
	<view><button open-type="getUserInfo" @getuserinfo="wxLogin">点我登录</button></view>
</template>

<script>
export default {
	methods: {
		wxLogin(userInfoRes) {
			console.log(userInfoRes);
			// 获取用户信息
			let { encryptedData, rawData, iv, signature } = userInfoRes.detail;
			//获取code
			uni.login({
				success: async loginRes => {
					let code = loginRes.code;

					let data = await this.$request({
						url: '/api/public/v1/users/wxlogin',
						method: 'POST',
						data: {
							code,
							encryptedData,
							rawData,
							iv,
							signature
						}
					});
					uni.setStorageSync('token', data.token);
					uni.setStorageSync('userInfo', userInfoRes.detail.userInfo);
					uni.showToast({
						title: '登录成功',
						duration:500,
						success: () => {
							// 并不是toast消失时候,是toast出现时候
							setTimeout(() => {
								uni.navigateBack();
							}, 500);
						}
					});
				}
			});
		}
	}
};
</script>

<style></style>
